github.com/nxtrace/NTrace-core@v1.3.1-0.20240513132635-39169291e8c9/fast_trace/fast_trace ipv6.go (about)

     1  package fastTrace
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/nxtrace/NTrace-core/ipgeo"
     6  	"github.com/nxtrace/NTrace-core/printer"
     7  	"github.com/nxtrace/NTrace-core/trace"
     8  	"github.com/nxtrace/NTrace-core/tracelog"
     9  	"github.com/nxtrace/NTrace-core/util"
    10  	"github.com/nxtrace/NTrace-core/wshandle"
    11  	"log"
    12  	"os"
    13  	"os/signal"
    14  )
    15  
    16  //var pFastTracer ParamsFastTrace
    17  
    18  func (f *FastTracer) tracert_v6(location string, ispCollection ISPCollection) {
    19  	fmt.Printf("%s『%s %s 』%s\n", printer.YELLOW_PREFIX, location, ispCollection.ISPName, printer.RESET_PREFIX)
    20  	fmt.Printf("traceroute to %s, %d hops max, %d byte packets\n", ispCollection.IPv6, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize)
    21  
    22  	ip, err := util.DomainLookUp(ispCollection.IPv6, "6", "", true)
    23  	if err != nil {
    24  		log.Fatal(err)
    25  	}
    26  	var conf = trace.Config{
    27  		BeginHop:         f.ParamsFastTrace.BeginHop,
    28  		DestIP:           ip,
    29  		DestPort:         80,
    30  		MaxHops:          f.ParamsFastTrace.MaxHops,
    31  		NumMeasurements:  3,
    32  		ParallelRequests: 18,
    33  		RDns:             f.ParamsFastTrace.RDns,
    34  		AlwaysWaitRDNS:   f.ParamsFastTrace.AlwaysWaitRDNS,
    35  		PacketInterval:   100,
    36  		TTLInterval:      500,
    37  		IPGeoSource:      ipgeo.GetSource("LeoMoeAPI"),
    38  		Timeout:          f.ParamsFastTrace.Timeout,
    39  		SrcAddr:          f.ParamsFastTrace.SrcAddr,
    40  		PktSize:          f.ParamsFastTrace.PktSize,
    41  		Lang:             f.ParamsFastTrace.Lang,
    42  	}
    43  
    44  	if oe {
    45  		fp, err := os.OpenFile("/tmp/trace.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, os.ModePerm)
    46  		if err != nil {
    47  			return
    48  		}
    49  		defer func(fp *os.File) {
    50  			err := fp.Close()
    51  			if err != nil {
    52  				log.Fatal(err)
    53  			}
    54  		}(fp)
    55  		log.SetOutput(fp)
    56  		log.SetFlags(0)
    57  		log.Printf("『%s %s 』\n", location, ispCollection.ISPName)
    58  		log.Printf("traceroute to %s, %d hops max, %d byte packets\n", ispCollection.IPv6, f.ParamsFastTrace.MaxHops, f.ParamsFastTrace.PktSize)
    59  		conf.RealtimePrinter = tracelog.RealtimePrinter
    60  	} else {
    61  		conf.RealtimePrinter = printer.RealtimePrinter
    62  	}
    63  
    64  	_, err = trace.Traceroute(f.TracerouteMethod, conf)
    65  
    66  	if err != nil {
    67  		log.Fatal(err)
    68  	}
    69  
    70  	fmt.Println()
    71  }
    72  
    73  func (f *FastTracer) testAll_v6() {
    74  	f.testCT_v6()
    75  	println()
    76  	f.testCU_v6()
    77  	println()
    78  	f.testCM_v6()
    79  	println()
    80  	f.testEDU_v6()
    81  }
    82  
    83  func (f *FastTracer) testCT_v6() {
    84  	f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CT163)
    85  	f.tracert_v6(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.CT163)
    86  	f.tracert_v6(TestIPsCollection.Hangzhou.Location, TestIPsCollection.Hangzhou.CT163)
    87  	f.tracert_v6(TestIPsCollection.Guangzhou.Location, TestIPsCollection.Guangzhou.CT163)
    88  }
    89  
    90  func (f *FastTracer) testCU_v6() {
    91  	f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CU169)
    92  	f.tracert_v6(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.CU169)
    93  	f.tracert_v6(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.CU9929)
    94  	f.tracert_v6(TestIPsCollection.Guangzhou.Location, TestIPsCollection.Guangzhou.CU169)
    95  }
    96  
    97  func (f *FastTracer) testCM_v6() {
    98  	f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CM)
    99  	f.tracert_v6(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.CM)
   100  	f.tracert_v6(TestIPsCollection.Hangzhou.Location, TestIPsCollection.Hangzhou.CM)
   101  	f.tracert_v6(TestIPsCollection.Guangzhou.Location, TestIPsCollection.Guangzhou.CM)
   102  }
   103  
   104  func (f *FastTracer) testEDU_v6() {
   105  	f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.EDU)
   106  	f.tracert_v6(TestIPsCollection.Shanghai.Location, TestIPsCollection.Shanghai.EDU)
   107  	f.tracert_v6(TestIPsCollection.Hangzhou.Location, TestIPsCollection.Hangzhou.EDU)
   108  }
   109  
   110  func (f *FastTracer) testFast_v6() {
   111  	f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CT163)
   112  	f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CU169)
   113  	f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.CM)
   114  	f.tracert_v6(TestIPsCollection.Beijing.Location, TestIPsCollection.Beijing.EDU)
   115  }
   116  
   117  func FastTestv6(tm bool, outEnable bool, paramsFastTrace ParamsFastTrace) {
   118  	var c string
   119  
   120  	oe = outEnable
   121  
   122  	fmt.Println("您想测试哪些ISP的路由?\n1. 国内四网\n2. 电信\n3. 联通\n4. 移动\n5. 教育网\n6. 全部")
   123  	fmt.Print("请选择选项:")
   124  	_, err := fmt.Scanln(&c)
   125  	if err != nil {
   126  		c = "1"
   127  	}
   128  
   129  	ft := FastTracer{
   130  		ParamsFastTrace: paramsFastTrace,
   131  	}
   132  
   133  	// 建立 WebSocket 连接
   134  	w := wshandle.New()
   135  	w.Interrupt = make(chan os.Signal, 1)
   136  	signal.Notify(w.Interrupt, os.Interrupt)
   137  	defer func() {
   138  		w.Conn.Close()
   139  	}()
   140  
   141  	if !tm {
   142  		ft.TracerouteMethod = trace.ICMPTrace
   143  		fmt.Println("您将默认使用ICMP协议进行路由跟踪,如果您想使用TCP SYN进行路由跟踪,可以加入 -T 参数")
   144  	} else {
   145  		ft.TracerouteMethod = trace.TCPTrace
   146  	}
   147  
   148  	switch c {
   149  	case "1":
   150  		ft.testFast_v6()
   151  	case "2":
   152  		ft.testCT_v6()
   153  	case "3":
   154  		ft.testCU_v6()
   155  	case "4":
   156  		ft.testCM_v6()
   157  	case "5":
   158  		ft.testEDU_v6()
   159  	case "6":
   160  		ft.testAll_v6()
   161  	default:
   162  		ft.testFast_v6()
   163  	}
   164  }